<?php


function mkdir_recursive($dirName){
    $newDir = "";
 foreach(split('/',dirname($dirName)) as $dirPart){
     if (! file_exists($newDir="$newDir$dirPart/"))
        mkdir($newDir);
 }
}
$contributor = $trxMnf->get("author");
 //"Grupa Atlantis Sp. z o.o. office@grupa-atlantis.pl\n     *                 http://www.grupa-atlantis.pl";
$version = $trxMnf->get("version");
$descr = $trxMnf->get("description");
$file_date = date("D d/m/y H:i:s");

function writeHeader($file_handle,$file_name) {

    global $contributor,$version,$file_date,$descr,$inc_sugar_entry;
fwrite($file_handle,"<?php\n\n");
fwrite($file_handle,"  /*********************************************************************************\n");
fwrite($file_handle,"  * File generated by the Sugar Translation Suite (Contributor : www.grupa-atlantis.pl)\n");
fwrite($file_handle,"  *********************************************************************************/\n");
fwrite($file_handle,"  /*********************************************************************************\n");
fwrite($file_handle,"  * \$Header: $file_name, $version $file_date \n");
fwrite($file_handle,"  * Description : $descr.\n");
fwrite($file_handle,"  * Contributor(s): $contributor\n");
fwrite($file_handle,"  *********************************************************************************/\n\n");

if (empty($inc_sugar_entry) || $inc_sugar_entry == "DEFINE") {
 
  fwrite($file_handle,"if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');\n\n");
} else if ($inc_sugar_entry == "GLOBALS") { 
  fwrite($file_handle,"if(empty(\$GLOBALS['sugarEntry'])) die('Not A Valid Entry Point');\n\n");
} 


}

function writeFooter($file_handle) {
    fwrite($file_handle,"\n\n");
}

function writeArrayContent($sugar_terms,$file_handle,$tab = '   ') {
     for ($j = 0 ; $j < count($sugar_terms); $j++) {
        $label_id = $sugar_terms[$j]->label_id;
        $text = ereg_replace("\\\\","",$sugar_terms[$j]->text);
        fwrite($file_handle,$tab."'".$label_id."' => '".ereg_replace("'","\'",$text)."' ");
        if ($j < count($sugar_terms) - 1) fwrite($file_handle, ",\n");
    }
}

function writeArray($version_id,$file,$array_id,$file_handle) {
     print "<TD>$file</TD><TD>$array_id</TD>";
     fwrite($file_handle, "\$".$array_id." = array ( \n");
     $sugar_terms = query_db("SELECT label_id, text FROM sugar_terms WHERE version_id=$version_id and lang = 'TRX' and file = '".$file."' and array_id = '".$array_id."' and sub_array_id = '' order by id");
     print "<TD>".count($sugar_terms)."</TD>";
     writeArrayContent($sugar_terms,$file_handle);

     $sugar_sub_arrays = query_db("SELECT distinct sub_array_id, level FROM sugar_terms WHERE version_id=$version_id and lang = 'TRX' and file = '".$file."' and array_id = '".$array_id."' and sub_array_id != ''");

     if (count($sugar_terms) > 0 && count($sugar_sub_arrays) > 0) fwrite($file_handle, ",\n");
     print "<TD>".count($sugar_sub_arrays)."</TD>";
     $array_terms_count = 0;
     for ($i = 0 ; $i < count($sugar_sub_arrays); $i++) {
         if($sugar_sub_arrays[$i]->level == 3) {
             $sub_array_id = $sugar_sub_arrays[$i]->sub_array_id;
             fwrite($file_handle,"   '".$sub_array_id."' => array (\n");
             $third_arrays = query_db("SELECT distinct third_array_id FROM sugar_terms WHERE version_id=$version_id and lang = 'TRX' and file = '".$file."' and array_id = '".$array_id."' and sub_array_id = '".$sub_array_id."'");
             for( $j =0; $j < count($third_arrays) ;$j ++) {
                 $third_array_id = $third_arrays[$j]->third_array_id;
                 fwrite($file_handle,"      '".$third_array_id."' => array (\n");
                 $third_terms = query_db("SELECT label_id, text FROM sugar_terms
                 WHERE version_id=$version_id and lang = 'TRX' and file = '".$file."'
                 and array_id = '".$array_id."' and sub_array_id = '".$sub_array_id."'
                 and third_array_id = '".$third_array_id."' order by id");
                 $array_terms_count += count($sugar_array_terms);
                 writeArrayContent($third_terms,$file_handle,"          ");
                 fwrite($file_handle,"  )");
                 if ($j < count($third_arrays) - 1) fwrite($file_handle, ",\n");
             }
             fwrite($file_handle,"  )");
         } else {
             $sub_array_id = $sugar_sub_arrays[$i]->sub_array_id;
             $sugar_array_terms = query_db("SELECT label_id, text FROM sugar_terms WHERE version_id=$version_id and lang = 'TRX' and file = '".$file."' and array_id = '".$array_id."' and sub_array_id = '".$sub_array_id."' order by id");
             $array_terms_count += count($sugar_array_terms);
             fwrite($file_handle,"   '".$sub_array_id."' => array (\n");
             writeArrayContent($sugar_array_terms,$file_handle,"      ");
             fwrite($file_handle,"  )");
         }
         if ($i < count($sugar_sub_arrays) - 1) fwrite($file_handle, ",\n");
     }
     fwrite($file_handle, ");\n");
     print "<TD>$array_terms_count</TD>";
}

$trxMnf->toFile($pack_path_prefix);

print "<BR>";
print "<TABLE align=center cellspacing=0 width='900px' class='tabform-list' border=1>";
print "<TR class='title'><TD colspan=5>Language Files</TD></TR>";
print "<TR><TD width=40%>File Name</TD><TD width=20%>Array Name</TD><TD width=40% colspan=3>Counts (Terms, Sub Arrays, Sub Array Terms) </TD></TR>";

$sugar_file = query_db("SELECT distinct file FROM sugar_terms WHERE version_id=$version_id and lang = 'TRX'");

//mkdir_recursive( $pack_path_prefix . 'include/language/'.$translationKey.'.lang.php');
$k = 0;
for ($i = 0 ; $i < count($sugar_file); $i++) {
    $file=$sugar_file[$i]->file;
    $file_name = $pack_path_prefix . $file;
    mkdir_recursive($file_name);
    $file_handle = fopen($file_name, 'w');
    writeHeader($file_handle,$file);
    $sugar_arrays = query_db("SELECT distinct array_id FROM sugar_terms WHERE version_id=$version_id and lang = 'TRX' and file = '".$file."'");
    for ($j = 0; $j < count($sugar_arrays);$j++) {
        $array_id = $sugar_arrays[$j]->array_id;
        if ($array_id) {
            print "<TR class=".(++$k % 2 ? "odd":"even")." >"; 
            writeArray($version_id,$file,$array_id,$file_handle);
            print "</TR>";
        }
    }
    writeFooter($file_handle);
    fclose($file_handle);
}

print "</TABLE>";
print "<BR>";